<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#include "../common/header.ftl">
    <link rel="stylesheet" href="/js/plugins/treeview/bootstrap-treeview.min.css" type="text/css">
    <script type="text/javascript" src="/js/plugins/treeview/bootstrap-treeview.min.js"></script>
    <script>
        $(function () {
            $.get('/region/listByParentId.do', function (data) {
                $('#treeview').treeview({
                    data: [{text: '全部地区', nodes: data}],
                    showTags: true,
                    lazyLoad: function (node) {
                        $.get('/region/listByParentId.do', {parentId: node.id}, function (data) {
                            $('#treeview').treeview('addNode', [data, node]);
                        });
                    },
                    onNodeSelected: function (event, data) {
                        $.get('/region/listByParentId.do', {parentId: data.id}, function (list) {
                            var temp = "";
                            $.each(list, function (index, ele) {
                                console.log(ele.json);
                                //temp+="<tr><td>"+(index+1)+"</td><td>"+ele.text+"</td><td><a href=''>"+"修改"+"</a></td></tr>";
                                temp += "<tr>\n" +
                                        "<td>" + (index + 1) + "</td>\n" +
                                        "<td>" + ele.text + "</td>\n" +
                                        "<td>\n" +
                                        "<a data-json='" + ele.json + "'\n" +
                                        " class=\"btn btn-info btn-xs inputBtn\" href=\"javascript:\" >\n" +
                                        " <span class=\"glyphicon glyphicon-pencil\"></span>编辑\n" +
                                        " </a>\n" +
                                        "</td>";
                            })
                            $('tbody').html(temp);
                            inputFun();
                        });
                    }
                });
            })
            inputFun();
            $('#editForm').ajaxForm(function (data) {
                if (data.success) {
                    window.location.reload();
                }else {
                    alert(data.msg);
                }
            });

            $('.submitBtn').click(function () {
                $('#editForm').submit();
            });
        });

        function inputFun() {
            $('.inputBtn').click(function () {
                $('#myModal input').val("");
                var obj = $(this).data('json');
                if (obj) {
                    $("#myModal input[name ='id']").val(obj.id);
                    $("#myModal input[name ='name']").val(obj.name);
                    $("#myModal select[name ='state']").val(obj.state);
                    $("#myModal input[name ='parent.id']").val(obj.parentId);
                    $("#myModal input[name ='parent.name']").val(obj.parentName);
                }else {
                    var node = $('#treeview').treeview('getSelected');
                    if (node.length >0){
                        $("#myModal input[name ='parent.id']").val(node[0].id);
                        $("#myModal input[name ='parent.name']").val(node[0].text);
                    }
                }
                $('#myModal').modal("show");
            });
        }
    </script>
</head>
<body>
<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-3">
            <#assign currentMenu = "region" />
            <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">地区管理</h1>
                </div>
            </div>
            <a class="btn btn-success inputBtn">
                <span href="javascript:" class="glyphicon glyphicon-plus"></span> 添加地区
            </a>
            <div class="row">
                <div class="col-lg-4">
                    <div id="treeview"></div>
                </div>
                <div class="col-lg-8">
                    <table class="table table-striped table-hover">
                        <thead>
                        <tr>
                            <th>序号</th>
                            <th>名称</th>
                        </tr>
                        </thead>
                        <tbody>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/region/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden" name="id">
                    <input type="hidden" name="parent.id" >
                    <div class="form-group">
                        <label class="col-sm-3 control-label">地区名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="name" placeholder="请输入地区/景区名称">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label">上级地区：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="parent.name"  readonly>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">状态：</label>
                        <div class="col-sm-6">
                            <select class="form-control" name="state">
                                <option value="0">普通</option>
                                <option value="1">推荐</option>
                            </select>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary submitBtn ">保存</button>
                <button type="button" class="btn btn-default " data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>
</body>
</html>